<template>
    <div class="app-container customer-div">
      <!--新增编辑奖惩-->
      <el-dialog
        :close-on-click-modal="false"
        title="奖惩记录"
        :visible.sync="dialogVisible3"
        width="420px">
        <el-form :model="jcForm" size="mini" label-width="100px" ref="jcForm" :rules="rules3">
          <el-form-item label="员工姓名：" prop="userId" v-if="source=='list'">
            <el-select v-model="jcForm.userId" filterable placeholder="请选择" clearable :disabled="popType3=='edit'">
              <el-option
                v-for="item in userOptionList"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="奖惩类型：" prop="status">
            <el-radio-group v-model="jcForm.status">
              <el-radio :label="1">奖励</el-radio>
              <el-radio :label="2">惩罚</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="奖惩时间：" prop="rewardTime">
            <el-date-picker
              style="width:100%"
              v-model="jcForm.rewardTime"
              type="date"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="奖惩内容：" prop="content">
            <el-input v-model="jcForm.content" type="textarea" :rows="2"></el-input>
          </el-form-item>
          <el-form-item label="奖惩金额：" prop="money">
            <el-input v-model="jcForm.money"></el-input>
          </el-form-item>
          <el-form-item label="奖惩原因：" prop="reason">
            <el-input v-model="jcForm.reason" type="textarea" :rows="2"></el-input>
          </el-form-item>
          <el-form-item label="奖惩备注：">
            <el-input v-model="jcForm.remark" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose3">取 消</el-button>
          <el-button type="primary" @click="handleConfirm3" :loading="btnLoading3">确 定</el-button>
      </span>
      </el-dialog>

      <!--撤消奖惩-->
      <el-dialog
        :close-on-click-modal="false"
        title="撤消奖惩"
        :visible.sync="dialogVisible4"
        width="420px">
        <div style="padding:20px;font-size: 14px">确认撤消此条奖惩记录吗？</div>
        <el-form :model="cxForm" size="mini" label-width="100px" ref="cxForm" :rules="rules4">
          <el-form-item label="撤消原因：" prop="revokeRemark">
            <el-input v-model="cxForm.revokeRemark" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose4">取 消</el-button>
          <el-button type="primary" @click="handleConfirm4" :loading="btnLoading4">确 定</el-button>
      </span>
      </el-dialog>
    </div>
</template>
<script>
    import {addReward,updateReward,revokeReward} from '@/api/company'
    import {getOrderUsersList} from '@/api/partner'
    const  defaultJCForm= {
      "userId": '',						//用户id
      "content": "",					//奖惩内容
      "money": "",						//奖惩金额
      "reason": "",						//奖惩原因
      "remark": "",						//备注
      "status": '',						//奖惩：1奖励，2惩罚
      "rewardTime": ""			//奖惩时间
    }
    const  defaultCXForm= {
      revokeRemark:'',
      rewardId:'',
    }
    export default {
        name: "gtpop",
        data(){

            return {
              jcForm:JSON.parse(JSON.stringify(defaultJCForm)),
              rules3:{
                userId:[{required: true, message: '此项必填', trigger: 'change'}],
                content:[{required: true, message: '此项必填', trigger: 'blur'},],
                money:[{required: true, message: '此项必填', trigger: 'blur'},],
                reason:[{required: true, message: '此项必填', trigger: 'blur'},],
                status:[{required: true, message: '此项必填', trigger: 'change'},],
                rewardTime:[{required: true, message: '此项必填', trigger: 'change'},],
              },
              btnLoading3:false,
              popType3:'',
              dialogVisible3:false,
              userOptionList:[],
              source:'',

              cxForm:JSON.parse(JSON.stringify(defaultCXForm)),
              rules4:{
                revokeRemark:[{required: true, message: '此项必填', trigger: 'blur'},],
              },
              btnLoading4:false,
              dialogVisible4:false,
            }
        },
        computed: {

        },
        components : {

        },
        created() {
          this.getUserOptionList();
        },
        methods:{
          //获取员工下拉选项
          getUserOptionList(){
            getOrderUsersList().then((res)=>{
              this.userOptionList = res;
            })
          },
          //显示新增/编辑奖罚记录
          showPop3(type,item,source){
            this.popType3 = type;
            this.source = source;
            if(type == 'add'){
              this.jcForm = JSON.parse(JSON.stringify(defaultJCForm));
            }else{
              this.jcForm = JSON.parse(JSON.stringify(item))
            }
            this.jcForm.userId = item.userId;
            this.dialogVisible3 = true;
          },

          handleClose3(){
            this.dialogVisible3 = false;
          },
          handleConfirm3(){
            this.$refs.jcForm.validate((valid) => {
              if (valid) {
                this.btnLoading3 = true;
                let data = JSON.parse(JSON.stringify(this.jcForm));
                let fn;
                if(this.popType3 == 'add'){
                  fn = addReward
                }else{
                  fn = updateReward
                }
                fn(data).then(()=>{
                  this.btnLoading3 = false;
                  this.handleClose3();
                  this.$emit('getShowRewardUser');
                }).catch(()=>{
                  this.btnLoading3 = false;
                })
              }
            })
          },


          //显示撤消奖罚记录
          showPop4(item){
            this.cxForm = JSON.parse(JSON.stringify(defaultCXForm))
            this.cxForm.rewardId = item.rewardId;
            this.dialogVisible4 = true;
          },

          handleClose4(){
            this.dialogVisible4 = false;
          },
          handleConfirm4(){
            this.$refs.cxForm.validate((valid) => {
              if (valid) {
                this.btnLoading4 = true;
                let data = JSON.parse(JSON.stringify(this.cxForm));
                revokeReward(data).then(()=>{
                  this.btnLoading4 = false;
                  this.handleClose4();
                  this.$emit('getShowRewardUser');
                }).catch(()=>{
                  this.btnLoading4 = false;
                })
              }
            })
          },

        },
    }
</script>

<style scoped lang="scss">

</style>
